package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.InsTacheRecord;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 11:46
 * @description
 */
@Repository("insTacheRecordArchiverDao")
public class insTacheRecordArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_TACHE_RECORD_INSTANCE WHERE TASK_INS_ID = ?");
        List<InsTacheRecord> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(InsTacheRecord.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `RECORD_ID`, `TASK_INS_ID`, `SOURCE_ID`, `TEMPLATE_ID`, `MO_ID`, `TACHE_TYPE`, `START_TIME`, `FINISH_TIME`, `DISPATCH_TIME`, `INDEX_GROUP_ID`, `DATA_ROWS`, `RECORD_TIME`, `PARTITION_CODE`, `BATCH_TIME`");
        sql.append(" FROM INS_TACHE_RECORD_INSTANCE WHERE TASK_INS_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<InsTacheRecord> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsTacheRecord insTacheRecord = new InsTacheRecord();
                insTacheRecord.setRecordId(resultSet.getLong("RECORD_ID"));
                insTacheRecord.setTaskInstId(resultSet.getLong("TASK_INS_ID"));
                insTacheRecord.setSourceId(resultSet.getLong("SOURCE_ID"));
                insTacheRecord.setTemplateId(resultSet.getLong("TEMPLATE_ID"));
                insTacheRecord.setMoId(resultSet.getLong("MO_ID"));
                insTacheRecord.setTacheType(resultSet.getString("TACHE_TYPE"));
                insTacheRecord.setStartTime(resultSet.getTimestamp("START_TIME"));
                insTacheRecord.setFinishTime(resultSet.getTimestamp("FINISH_TIME"));
                insTacheRecord.setDispatchTime(resultSet.getString("DISPATCH_TIME"));
                insTacheRecord.setIndexGroupId(resultSet.getLong("INDEX_GROUP_ID"));
                insTacheRecord.setDataRows(resultSet.getString("DATA_ROWS"));
                insTacheRecord.setRecordTime(resultSet.getTimestamp("RECORD_TIME"));
                insTacheRecord.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                insTacheRecord.setBatchTime(resultSet.getString("BATCH_TIME"));
                return insTacheRecord;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_tache_record_instance`");
        sql.append(" (`RECORD_ID`, `TASK_INS_ID`, `SOURCE_ID`, `TEMPLATE_ID`, `MO_ID`, `TACHE_TYPE`, `START_TIME`, `FINISH_TIME`, `DISPATCH_TIME`, `INDEX_GROUP_ID`, `DATA_ROWS`, `RECORD_TIME`, `PARTITION_CODE`, `BATCH_TIME`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:recordId,:taskInstId,:sourceId,:templateId,:moId,:tacheType,:startTime,:finishTime,:dispatchTime,:indexGroupId,:dataRows,:recordTime,:partitionCode,:batchTime,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {

    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_TACHE_RECORD_INSTANCE WHERE RECORD_ID = :recordId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
